﻿using Kimd.Motion;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WorkStation.Common;

namespace WorkStation.UI
{
    public partial class FormKeyboard : Form
    {
        private double _step = 0.1;
        public Axis OperateAxis1 { get; set; }
        public Axis OperateAxis2 { get; set; }
        public FormKeyboard()
        {
            InitializeComponent();
        }

        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void t_Axis_Tick(object sender, EventArgs e)
        {
            if (OperateAxis1 != null)
            {
                l_X.Visible = true;
                l_X.Text = $"{OperateAxis1.Description}当前位置: {OperateAxis1.Position.ToString("0.000").PadLeft(8, ' ')}";
            }
            else
                l_X.Visible = false;
            if (OperateAxis2 != null)
            {
                l_Y.Visible = true;
                l_Y.Text = $"{OperateAxis2.Description}当前位置: {OperateAxis2.Position.ToString("0.000").PadLeft(8, ' ')}";
            }
            else
                l_Y.Visible = false;
        }

        private void FormKeyboard_KeyPress(object sender, KeyPressEventArgs e)
        {
        }
        protected override bool ProcessDialogKey(Keys keycode)
        {
            string errInfo = "";
            switch (keycode)
            {
                case Keys.Left:
                    OperateAxis1.SetSpeed(SpeedType.Manual);
                    OperateAxis1.MoveAbsolute(OperateAxis1.Position - _step, out errInfo, 0);
                    break;
                case Keys.Up:
                    OperateAxis2.SetSpeed(SpeedType.Manual);
                    OperateAxis2.MoveAbsolute(OperateAxis2.Position - _step, out errInfo, 0);
                    break;
                case Keys.Right:
                    OperateAxis1.SetSpeed(SpeedType.Manual);
                    OperateAxis1.MoveAbsolute(OperateAxis1.Position + _step, out errInfo, 0);
                    break;
                case Keys.Down:
                    OperateAxis2.SetSpeed(SpeedType.Manual);
                    OperateAxis2.MoveAbsolute(OperateAxis2.Position + _step, out errInfo, 0);
                    break;
                case Keys.Escape:
                    btn_Exit.PerformClick();
                    break;
            }
            if(!string.IsNullOrEmpty(errInfo))
                SystemInfo.ShowToastInfo(this, errInfo, true);
            return true;
        }

        private void btn_Step_Click(object sender, EventArgs e)
        {
            if (_step == 0.1)
                _step = 0.2;
            else if (_step == 0.2)
                _step = 0.5;
            else if (_step == 0.5)
                _step = 1;
            else if (_step == 1)
                _step = 2;
            else if (_step == 2)
                _step = 5;
            else if (_step == 5)
                _step = 0.01;
            else if (_step == 0.01)
                _step = 0.02;
            else if (_step == 0.02)
                _step = 0.05;
            else if (_step == 0.05)
                _step = 0.1;
            btn_Step.Text = $"寸动精度:{_step.ToString().PadLeft(4,' ')}mm";
        }

        private void FormKeyboard_Load(object sender, EventArgs e)
        {
            btn_Step.Text = $"寸动精度:{_step.ToString().PadLeft(4, ' ')}mm";
            if (OperateAxis1 != null)
            {
                l_HNeg.Visible = true;
                l_DirHNeg.Visible = true;
                l_HNeg.Text = $"{OperateAxis1.Description}-";
                l_HPos.Visible = true;
                l_DirHPos.Visible = true;
                l_HPos.Text = $"{OperateAxis1.Description}+";
            }
            if (OperateAxis2 != null)
            {
                l_VNeg.Visible = true;
                l_DirVNeg.Visible = true;
                l_VNeg.Text = $"{OperateAxis2.Description}-";
                l_VPos.Visible = true;
                l_DirVPos.Visible = true;
                l_VPos.Text = $"{OperateAxis2.Description}+";
            }
        }
    }
}
